package com.haiermzj.client.fragments;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.haiermzj.client.R;
import com.haiermzj.client.SocketConnect;
import com.haiermzj.client.TestSocketCallback;
import com.haiermzj.client.activity.MainActivity;
import com.haiermzj.client.activity.TakePicActivity;
import com.haiermzj.client.utils.Contance;

/**
 * Created by wanghaixin on 17/9/26.
 */

public class MainFragment extends Fragment implements View.OnClickListener {
    public static String TAG = MainFragment.class.getSimpleName();
    private View inflate;
    private MainActivity activity;
    private TextView weightValue;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.activity = (MainActivity) context;
    }
/*----*/

    //android模拟器把127.0.0.1当做自己的IP， 把10.0.2.2就能成功访问你的电脑本机
    public static final String HOST = "192.168.199.105";

    // 远程tcp socket服务端口
    public static final int PORT = 8888;


    // 封装的socket byte[]收发
    private TestSocketCallback callback;
    private SocketConnect connect;
    private Button openCamera;
    /*----*/
    /**
     * 接收线程发送过来的消息，并用TextView显示
     */
    private static Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i(TAG, "handleMessage----");

//            image.setImageBitmap(((Bitmap) msg.obj));
            String str = (String) msg.obj;
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SocketConnect();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_main_layout, null);
        TextView startPic = (TextView) inflate.findViewById(R.id.start_pic);
        weightValue = (TextView) inflate.findViewById(R.id.weight_value);
        /*设置 同一TextView值 不同大小字体*/
        SpannableString styledText = new SpannableString("77.8 kg");
        styledText.setSpan(new TextAppearanceSpan(getContext(), R.style.style_text_span_0), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        weightValue.setText(styledText, TextView.BufferType.SPANNABLE);
        startPic.setOnClickListener(this);

        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ((TextView) inflate.findViewById(R.id.title)).setText("首页");

    }


    private void SocketConnect() {
//        Log.i(TAG, "SocketConnect----");

        if (connect == null) {
            callback = new TestSocketCallback(mhandler);
            connect = new SocketConnect(callback, getContext());
            connect.setRemoteAddress(HOST, PORT);
            Contance.connect = connect;
            new Thread(connect).start();

        } else {
            ShowDialog("socket is not null, no need connect again!");
        }
    }


    private void socketSendStr(String msg) {
//        Log.i(TAG, "socketSendStr----");
        if (connect != null) {
            connect.send(msg.getBytes());
        } else {
            ShowDialog("socketConnect is null, need init！");
        }
    }

    private void socketClose() {
//        Log.i(TAG, "socketClose----");

        if (connect != null) {
            connect.disconnect();
            connect = null;
        }/*else{
            ShowDialog("socketConnect is null, not need close！");
		}*/
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        socketClose();
        Log.e(TAG, "onDestroy: ");
    }

    /**
     * @param msg
     * @return void
     * @Description: 弹出AlertDialog
     */
    public void ShowDialog(String msg) {
        new AlertDialog.Builder(activity)
                .setTitle("提示")
                .setMessage(msg)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_pic:
                Toast.makeText(getContext(), "打开拍照页面", Toast.LENGTH_SHORT).show();

                socketSendStr(Contance.OPEN_CAMERA);//给镜子发送指令打开相机
                 /*打开拍照Activity*/
                Intent intent = new Intent(getActivity(), TakePicActivity.class);
                startActivity(intent);
                break;
        }
    }

}
